/************************* 版权声明 *********************************
 * 
 * 版权所有：Apache License 2.0
 * 
 ************************* 版本声明 *********************************
 *@since jdk1.5
 *@version 0.1
 ************************* 变更记录 *********************************
 *
 * 创建者：bijiqiang Email:bijiqiang@gmail.com   创建日期： 2009-12-23
 * 创建记录：创建类结构。
 * 
 * 修改者：       修改日期：
 * 修改记录：
 *
 * 修改者：       修改日期：
 * 修改记录：
 * ……
 ************************* To  Do *********************************
 *
 * 1、实现所有未实现的方法（抛出UnsupportedOperationException的方法）。
 * 
 ************************* 随   笔 *********************************
 *
 * 
 * 
 ******************************************************************
 */

package com.realgodo.site.service.application;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.application.favorite.Favorite;
import com.realgodo.site.entity.application.show.Show;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.service.space.SpaceManager;

@Service("favoriteManager")
@Transactional
public class FavoriteManager extends DefaultEntityManager<Favorite, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	@Autowired
	private ShowManager showManager;

	@Autowired
	private SpaceManager spaceManager;

	/**
	 * 创建一个收藏
	 * 
	 * @param showId
	 * @param spaceId
	 * @return
	 */
	public Favorite createFavorite(Long showId, Long spaceId) {
		if (this.getFavoriteProbability(showId, spaceId)) {
			Favorite favorite = new Favorite();
			Space space = this.spaceManager.get(spaceId);
			Show show = this.showManager.get(showId);
			show.setFavoriteCount(show.getFavoriteCount() + 1);
			show = (Show) this.showManager.saveMerge(show);// 增加收藏数量
			favorite.setClasz(Favorite.class);
			favorite.setShow(show);
			favorite.setSpace(space);
			return (Favorite) this.saveMerge(favorite);
		} else {
			return null;
		}

	}

	/**
	 * 获取某个用户的
	 * 
	 * @param list
	 * @param space
	 * @return
	 */
	public Page<Favorite> findFavorites(Page<Favorite> list, Space space) {

		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Favorite.class);

		criteria.add(Restrictions.eq("space.id", space.getId()));
		criteria.addOrder(Order.desc("pubTime"));

		return super.getEntityDao().findByCriteria(list, criteria);
	}

	public void removeFavorite(Long id) {
		this.entityDao.delete(id);
	}

	public boolean getFavoriteProbability(Long showId, Long spaceId) {
		boolean flag = false;
		Show show = showManager.get(showId);
		if (!show.getSpace().getId().equals(spaceId)) {// 不能收藏自己的展示
			flag = !this.hasSameFavorite(showId, spaceId);
		}
		return flag;
	}

	public boolean hasSameFavorite(Long showId, Long spaceId) {
		boolean flag = false;
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Favorite.class);
		flag = (c.list().size() > 0 ? true : false);
		return flag;
	}
}
